package cn.panshi.mongocdc;

import java.lang.reflect.Method;
import java.util.List;

import org.bson.Document;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.model.changestream.OperationType;

/**
 *  Define a group of API ,like below ,each of which used to handle one type of scheduled business. <br/>
 *  
 *  cn：按如下示例定义一组API，分别用来处理与E中的枚举值对应的业务类型。<br/>
 *  
 *  //@param bizId: the id of record associated with current cdc event
 *  //@param resumeToken: the id of current cdc event,can be used to resume cdc listening on application reboot
 *  //@param doc: the record associated with current cdc event,unserialized from cdc event.fullDocument
 *  @BizRouterAnnotation(enumClazz = AppBizEnum.class, enumValue = "BizXxx") <br/>
    void doBizXxx(String bizId, String resumeToken,Message<ChangeStreamDocument<Document>, Document> message) { <br/>
		log.debug("BizXxx cdc " + doc);<br/>
    } <br/>
 */
@SuppressWarnings("rawtypes")
public abstract class IMongoCdcBizRouter<E extends Enum> {

	public abstract boolean activeByProfile();

	public abstract Class<E> getBizEnumType();

	public abstract E[] getBizEnumValues();

	public abstract List<E> parse(String collectionName, OperationType operationType);

	public abstract List<Method> matchBizMethodByCdc(Enum cdc);

	public static <T> T unserializeDocument(Document doc, Class<T> clazz) {
		String json = JSONObject.toJSONString(doc);
		T vo = JSONObject.parseObject(json, clazz);
		return vo;
	}
}
